package com.example.game.app.controller;


import com.example.game.app.domain.*;
import com.example.game.module.entity.Category;
import com.example.game.module.entity.Game;
import com.example.game.module.service.CategoryService;
import com.example.game.module.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/game")
public class GameController {

    @Autowired
    private  GameService gameService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public GameListVO getGameList (@RequestParam Integer page, @RequestParam(required = false) String gameName) {
        int pageSize = 5;

        List<Game> games = gameService.getAllGames(page,pageSize,gameName);
        Boolean isEnd = games.size() < pageSize;

        // 收集所有categoryId
        List<BigInteger> categoryIds = games.stream()
				.map(Game::getCategoryId)
				.distinct()
				.collect(Collectors.toList());

		// 批量查询分类信息
		List<Category> categories = categoryService.listByIds(categoryIds);
		Map<BigInteger, Category> categoryMap = categories.stream()
				.collect(Collectors.toMap(Category::getId, Function.identity()));

		List<GameItemVO> gameItems = games.stream()
                .map(game -> {
                    GameItemVO itemVO = new GameItemVO();
                    itemVO.setId(game.getId());
                    itemVO.setImage(game.getCoverImages());
                    itemVO.setGameName(game.getGameName());
                    itemVO.setPlayerNumber(game.getPlayerNumber());
                    BigInteger categoryId = game.getCategoryId();
                    Category category = categoryMap.get(categoryId);
                    if (category != null) {
                        itemVO.setCategoryName(category.getName());
                    } else {
                        // 处理分类不存在的情况
                        itemVO.setCategoryName("未知分类");
                    }
                    return itemVO;
                })
                .collect(Collectors.toList());

        GameListVO gameListVO = new GameListVO();
        gameListVO.setList(gameItems);
        gameListVO.setIsEnd(isEnd);

        return gameListVO;
    }

    @GetMapping("/info")
    public GameInfoVo getGameInfo(@RequestParam BigInteger gameId) {
        Game game = gameService.getById(gameId);

        if (game == null) {
            return null;
        }

        Category category = categoryService.getById(gameId);
        String categoryName = category != null ? category.getName() : null;
        String categoryImage = category != null ? category.getImage() : null;

        GameInfoVo gameInfoVO = new GameInfoVo();
        gameInfoVO.setTitle(game.getTitle());
        gameInfoVO.setCreateTime(game.getCreateTime());
        gameInfoVO.setCoverImages(Collections.singletonList(game.getCoverImages()));
        gameInfoVO.setGameName(game.getGameName());
        gameInfoVO.setReleaseDate(game.getReleaseDate());
        gameInfoVO.setPrice(game.getPrice());
        gameInfoVO.setGameDescription(game.getGameDescription());
        gameInfoVO.setRating(game.getRating());
        gameInfoVO.setCategoryName(categoryName);
        gameInfoVO.setCategoryImage(categoryImage);

        return gameInfoVO;
    }

    @GetMapping("/category")
    public CategoryListVO getCategoryList() {
        List<Category> categories = categoryService.findAll();
        List<CategoryItemVO> categoryItemVOS = categories.stream()
                .map(category -> {
                    CategoryItemVO itemVO = new CategoryItemVO();
                    itemVO.setId(category.getId());
                    itemVO.setName(category.getName());
                    itemVO.setImage(category.getImage());
                    return itemVO;
                })
                .collect(Collectors.toList());

        CategoryListVO categoryListVO = new CategoryListVO();
        categoryListVO.setList(categoryItemVOS);

        return categoryListVO;
    }

}
